import { execSync } from 'child_process'
import { update as Update } from '../../other/update.js'

export class Lain extends plugin {
  constructor () {
    super({
      name: '铃音基本设置',
      priority: -50,
      rule: [
        {
          reg: /^#(Lain|铃音)(强制)?更新(日志)?$/gi,
          fnc: 'update',
          permission: 'master'
        }
      ]
    })
  }

  async update (e) {
    let new_update = new Update()
    new_update.e = e
    new_update.reply = this.reply
    const name = 'Lain-plugin'
    if (e.msg.includes('更新日志')) {
      if (new_update.getPlugin(name)) {
        this.e.reply(await new_update.getLog(name))
      }
    } else {
      if (new_update.getPlugin(name)) {
        if (this.e.msg.includes('强制')) { execSync('git reset --hard', { cwd: `${process.cwd()}/plugins/${name}/` }) }
        await new_update.runUpdate(name)
        if (new_update.isUp) { setTimeout(() => new_update.restart(), 2000) }
      }
    }
    return true
  }
}
